<?php 
namespace app\index\controller;

use \think\Controller;
use \think\Request;
use \think\Session;
use app\common\controller\Api as commonApi;
use app\user\traits\controllers\TraitUser;
/**
 * 前台预定流程(已筛出资源)
 */
class Reserve extends commonApi
{
    use TraitUser;
    use \app\user\traits\models\UserInfoCheck;
    const DEPOSIT = '200';//押金
    const RATIO   = '1:1';//旅行币与人民币比率
    //前置操作
    protected $beforeActionList = [
        
    ];
    ///////////////////////交换房车流程（包括出租）///////////////////////////
    /**
     * [getCaravanDetail 资源详情界面]
     * @return [type] [description]
     */
    public function getCaravanDetail()
    { 
        //房车id
        Session::set('redirect_url', Request::instance()->url(true));
        $id = input('id');
        $ee = input('edate'); 
        $ss = input('sdate');
        $this->assign('ee',$ee); $this->assign('ss',$ss);
        $str1=strtotime($ee);$str2=strtotime($ss);$days = ($str1-$str2)/(24*60*60)+1;
        $this->assign('days',$days);
        //获取状态数组
        $share = model('Caravans')->getCaravanStatus($id);
        $this->assign('share',$share['shares']);//获取日历表房车的可用状态
        $this->assign('disables',$share['disables']);//获取日历表房车的订单状态
        $this->assign('trusts',$share['trusts']);//获取日历表房车的托管状态
        //价格显示问题
        $arr   = model('Reserves')->calculatePrice($id, $ss, $ee);
        $price = $this->price_array($arr);
        $this->assign('price',json_encode($price));
        //价格显示问题
        //押金
        $deposit = self::DEPOSIT;  $this->assign('deposit',$deposit);
        $carinfo = model('CaravanInfos')->where('id',$id)->find();
        $camp_id = $carinfo->services; $user_id = $carinfo->user_id;
        //营地信息
        $campinfo = model('common/camps')->where('id',$camp_id)->find();
        //房车拥有者头像
        $head = db('users')->where('id',$user_id)->value('head');
        //房车图片
        $a=$carinfo->piccaravan;
        $picCaravan= $this->getEscapeSlash($a);
        $this->assign('picCaravan',$picCaravan);
        //房车封面
        $b = $carinfo->caravancover;
        $caravancover= $this->getEscapeSlash($b);
        $this->assign('caravancover',$caravancover);
        //钱数计算
        $money = model('Orders')->getCost($id,$ss,$ee);
        //$money = ['intergral_total'=>'100','money_total'=>'200'];测试
        $this->assign('money',$money);
        return view('caravandetail',['carinfo'=>$carinfo,'campinfo'=>$campinfo,'head'=>$head]);
    }
    /**
     * [price_array 价格数组]
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    public function price_array($data)
    {
        $zy = ['travel_coin'=>'旅行币','total_money'=>'人民币'];
        $arr = [];
        if (is_array($data)) {
            foreach ($data as  $value) {
                $arr[$value['datetimes']] = $value['amount'].$zy[$value['types']];
            }
        }
        return $arr;
    }
    /**
     * [getJumpWhich 判断登陆状态的跳转界面]
     * @return [type] [description]
     */
    public function getJumpWhich()
    { 
        $id = input('id');$ee = input('edate'); $ss = input('sdate');//halt($id);
        //判断是否有不可住日期
        $query = model('Calendar')->where('caravan_id', $id)->where('datetimes', 'between', [$ss, $ee]);
        $res = $query->where(function($query1){
            $query1->whereOr(['orders' => ['in', '1,2'],'shares'=>0]);
        })->count() ? 0 : 1;
        if (!$res) {
            return $this->error('亲，包含不可住日期',$url = null,'',$wait=3);
        }
        if (!empty(session('user.id'))) {
            $this->redirect("/reserve/logorderinfo?id={$id}&sdate={$ss}&edate={$ee}");
            exit;
        }else{
            return $this->error('请登录您的账号',url('/user/login','','','user'));
        }
    }
    /**
     * [getLogOrderInfo 预定界面填写订单(登录状态)]
     * @return [type] [description]
     */
    public function getLogOrderInfo()
    {
        //房车id获取房车信息
        $id = input('id');$ee = input('edate'); $ss = input('sdate');//dump($id);
        $deposit =self::DEPOSIT;
        $ratio   =self::RATIO;
        $this->assign('ratio',$ratio);
        $carinfo = model('CaravanInfos')->where('id',$id)->find();
        $str1=strtotime($ee);$str2=strtotime($ss);$days= ($str1-$str2)/(24*60*60)+1;
        //获取登录者联系人信息
        $user_id = session('user.id');
        //$user_id = 2;//测试
        $contacts = db('contactioninfo')->where('user_id',$user_id)->select();
        $this->assign('contacts',$contacts);
        //房车图片
        $a=$carinfo->piccaravan;

        $picCaravan= $this->getEscapeSlash($a);
        $this->assign('picCaravan',$picCaravan);
        //总旅行币
        $camp_id =db('caravan')->where('id',$id)->value('services');
        //钱数
        $money = model('Orders')->getCost($id,$ss,$ee);
        //$money = ['intergral_total'=>'100','money_total'=>'200'];测试
        $this->assign('money',$money);
        //登录者旅行币多少
        $integral = db('deposit')->where(['user_id'=>session("user.id"),'types'=>'integral'])->value("amount");
        $integral = $integral ? $integral : 0;
        //$integral = db('users')->where('id',$user_id)->value('integral');
        $this->assign('integral',$integral);
        return view('orderinfologin',['deposit'=>$deposit,'carinfo'=>$carinfo,'ee'=>$ee,'ss'=>$ss,'days'=>$days]);
    }
    //提交预定界面即生成订单（测试）
    public function getPay()
    {
        return view('paychose');
    }
    /**
     * [getCheckEmail 校验字段问题]
     * @return [type] [description]
     */
    public function getCheckEmail()
    {
        $email = input('email');
        $re = $this->cheack(['email'=>$email]);
        if ($re) {
            return 1;
        }else{
            return json(0);
        }
    }
    public function getCheckCode()
    {
        $code=input('imgCode');
        $captcha = new \think\captcha\Captcha(config('captcha'));
        if (!$captcha->check($code)) {
            return json(0);
        }
        return 1;
    }
    public function getCheckUserLogin()
    {
        if (!session('user.id')) {
            return json(0);
        }
        return 1;
    }
    /**
     * [getCheckDateAvailable 判断是否可住]
     * @return [type] [description]
     */
    public function getCheckDateAvailable()
    
    {
        $start_date = input('sdate');
        $end_date = input('edate');$caravan_id = input('id');
        $end_date = $end_date ? : $start_date;
        //房车id
        $query = model('Calendar')->where('caravan_id', $caravan_id)->where('datetimes', 'between', [$start_date, $end_date]);
        //出租于分享无判定价值
        return  $query->where(function($query1){
            $query1->whereOr(['orders' => ['in', '1,2'],'shares'=>0]);
        })->count() ? 0 : 1;
    }
    /**
     * [getCountDays 天数与钱数]
     * @return [type] [description]
     */
    public function getCountDays()
    {
        $ss = input('sdate');$ee = input('edate');//$travel_money_base = input('m');
        $caravan_id = input('id');
        $str1=strtotime($ee);$str2=strtotime($ss);
        //时间天数
        $days= ($str1-$str2)/(24*60*60)+1;
        //$money = model('Orders')->getIntergral($caravan_id,$ss,$ee,$travel_money_base);
        $money = model('Orders')->getCost($caravan_id,$ss,$ee);
        $data = ['days'=>$days,'intergral'=>$money['intergral_total'],'money'=>$money['money_total']];
        return $data;
    }
    //////////////////////////////转义斜杠///////////////////////////
    public function getEscapeSlash($data_arr)
    {
        $data = str_replace("\\","/",$data_arr);
        return $data;
    }
    
}